$.ajax({
    type: 'POST',
    url: '/isLogin',
    async: false,
    data: {},
    dataType: 'json',
    success: function (result) {
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status==555){
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                    top.location.href="/login";
                });
            });
        }else{
            console.log(XMLHttpRequest.status)
            var layer;
            layui.use(['layer', 'form'], function(){
                layer = layui.layer
                layer.alert('页面加载失败',{icon:7}, function(index){
                    //do something
                    layer.close(index);
                });
            });;
        }
    },
});
var width=$('.layui-table-body').width();
layui.use('table', function(){
    // laydate.render({
    //     elem: '#demoReload'
    //     ,type:'month'
    // });
    var table = layui.table ,form = layui.form;
    //监听状态操作
    form.on('switch(statusUser)', function(obj){
        var id = this.value;
        var stu = $(this).attr("stuval");
        console.log(id);
        console.log(stu);
        if (stu==1){
            $(this).attr("stuval","0")
        }else if (stu==0){
            $(this).attr("stuval","1")
        }
        $.ajax({
            type: 'POST',
            url: '/system/updateStatus',
            async: false,
            data: {"id":id,"status":stu},
            dataType: 'json',
            success: function (result) {

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
        //alert(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
    });
    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        ,url: "/system/userList"
        ,cols: [[
            // {field:'id', title: '序号', width:width*0.25 }
            {field:'id', title: '用户标识', width:100}
            ,{field:'username', title: '用户名', width:200}
            ,{field:'nickname', title: '用户昵称', width:350}
            ,{field:'tel', title: '联系方式', width:350}
            ,{field:'status', title:'状态', width:200, templet: '#switchUser', unresize: true}
            ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:400}
        ]]
        ,id: 'testReload'
        ,page: true
        ,height: 522
        ,response: {
            statusCode: 666
        }
        // ,even:true //每行条纹
        ,limits: [10,15,20,25,30]
        ,limit: 10 //每页默认显示的数量
        ,toolbar:'#toolbarDemo'  //导出打印
        ,parseData:function (res) {
            return{
                "code":res.code,
                "msg":res.msg,
                "count":res.data.count,
                "data":res.data.userList
            };
        }
        , done: function(res, curr, count){
            // $(".layui-table-box").find("[data-field='id']").css("display","none");

      /*      $("[data-field='state']").children().each(function(){
                if($(this).text()=='1'){
                    $(this).text("启用")
                }else if($(this).text()=='0'){
                    $(this).text("未启用");
                }
            });*/
        }

    });

    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    ym: demoReload.val()
                }
            });
        }
    };

    $('#query').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听行单击事件（单击事件为：rowDouble）
    //其中的user对应的是 table中的lay-filter="user"
    table.on('row(user)', function(obj){
        var data = obj.data;
        //标注选中样式
        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    });
    table.on('tool(user)', function(obj) {
        var user = obj.data;
        $("#userId").val(user.id);
        form.val('example', {
            "username": user.username
            ,"roleid": user.roleid
            ,"name": user.name
            ,"tel": user.tel
            ,"position": user.position
        });
        if(obj.event === 'modify'){
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                $('#modify_user').css("display","block");
                layer.open({
                    type: 1,
                    title: ['用户修改', 'font-size:17px;font-weight:bolder;background-color:#796AEE;color:white;height:50px;margin-top:-2px'],
                    // skin: 'layui-layer-rim', //加上边框
                    area: ['600px', '550px'], //宽高
                    content:$('#modify_user'),
                    end: function () {
                        $('#modify_user').css("display","none");
                    }
                })
            });
        }else if(obj.event === 'resource'){
            table.render({
                elem: '#LAY_table_resource'
                ,url:'/system/getUserResource?id='+user.id
                ,cols: [[
                    {field:'name', width:150, title: '资源名称'}
                    ,{field:'url', width:240, title: '资源URL'}
                    ,{field:'permission', width:200, title: '资源权限字符串'}
                ]]
            });
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                $('#user_resource').css("display","block");
                layer.open({
                    type: 1,
                    title: ['权限资源查询', 'font-size:17px;font-weight:bolder;background-color:#796AEE;color:white;height:50px;margin-top:-2px'],
                    // skin: 'layui-layer-rim', //加上边框
                    scrollbar: false,
                    area: ['600px', '500px'], //宽高
                    content:$('#user_resource'),
                    end: function () {
                        $('#user_resource').css("display","none");
                    }
                })
            });
        }else if(obj.event === 'del'){
            layer.confirm('确定要删除该该用户吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type: 'POST',
                    url: '/system/delUser',
                    async: false,
                    data: {"id":user.id},
                    dataType: 'json',
                    success: function (result) {
                        if (result.code==666){
                            layer.alert('删除成功', {icon: 1},function (index) {
                                layer.close(index);
                                window.location.reload()
                            });

                        }else{
                            layer.alert('删除失败', {icon: 2},function (index) {
                                layer.close(index);
                                window.location.reload()
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (XMLHttpRequest.status==555){
                            var layer;
                            layui.use(['layer', 'form'], function(){
                                layer = layui.layer
                                layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                                    //do something
                                    layer.close(index);
                                    top.location.href="/login";
                                });

                            });

                        }else{
                            var layer;
                            layui.use(['layer', 'form'], function(){
                                layer = layui.layer
                                layer.alert('页面加载失败',{icon:7}, function(index){
                                    //do something
                                    layer.close(index);
                                });
                            });
                        }
                    },
                });

            }, function(){

            });
        }
    })

});
function modifyUser() {
    //var form = document.getElementById("userModifyForm");
    var userID = $("#userId").val();
    var username = $("#username").val();
    var roleid = $("#roleid").val();
    var name = $("#name").val();
    var tel = $("#tel").val();
    var position = $("#position").val();
    var pattern = /^(13|14|15|18|17)[0-9]{9}$/;
     if(username==null||username===""){
         layer.msg('用户名不能为空', {icon: 2,time:2000});
    }else if(roleid===""){
         layer.msg('角色未选择', {icon: 2,time:2000});
     }else if(name==null||name===""){
         layer.msg('联系人不能为空', {icon: 2,time:2000});
     }else if(!pattern.test(tel)){
         layer.msg('手机号不正确', {icon: 2,time:2000});
     }else{
         $.ajax({
             type: 'POST',
             url: '/system/updateUser',
             async: false,
             data: {"id":userID,"username":username,"roleid":roleid,"name":name,"tel":tel,"position":position},
             dataType: 'json',
             success: function (result) {
                 if (result.code==666){
                     layer.alert('修改成功', {icon: 1},function (index) {
                         layer.closeAll();
                         $('#modify_user').css("display","none");
                         window.location.reload()

                     });

                 }else{
                 }
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 if (XMLHttpRequest.status==555){
                     var layer;
                     layui.use(['layer', 'form'], function(){
                         layer = layui.layer
                         layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                             //do something
                             layer.close(index);
                             top.location.href="/login";
                         });
                     });
                 }else{
                     var layer;
                     layui.use(['layer', 'form'], function(){
                         layer = layui.layer
                         layer.alert('页面加载失败',{icon:7}, function(index){
                             layer.close(index);
                         });
                     });
                 }
             },
         });
     }

}
function userAddView() {
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer;
        $('#add_user').css("display","block");
        layer.open({
            type: 1,
            title: ['用户添加', 'font-size:17px;font-weight:bolder;background-color:#796AEE;color:white;height:50px;margin-top:-2px'],
            // skin: 'layui-layer-rim', //加上边框
            area: ['600px', '550px'], //宽高
            content:$('#add_user'),
            end: function () {
                $('#add_user').css("display","none");
            }
        })
    });
}
function addUser() {
    //var form = document.getElementById("userModifyForm");
    var userID = $("#add_userId").val();
    var username = $("#add_username").val();
    var roleid = $("#add_roleid").val();
    var name = $("#add_name").val();
    var tel = $("#add_tel").val();
    var position = $("#add_position").val();
    var pattern = /^(13|14|15|18|17)[0-9]{9}$/;
    if(username==null||username===""){
        layer.msg('用户名不能为空', {icon: 2,time:2000});
    }else if(roleid===""){
        layer.msg('角色未选择', {icon: 2,time:2000});
    }else if(name==null||name===""){
        layer.msg('联系人不能为空', {icon: 2,time:2000});
    }else if(!pattern.test(tel)){
        layer.msg('手机号不正确', {icon: 2,time:2000});
    }else{
        $.ajax({
            type: 'POST',
            url: '/system/addUser',
            async: false,
            data: {"id":userID,"username":username,"roleid":roleid,"name":name,"tel":tel,"position":position},
            dataType: 'json',
            success: function (result) {
                if (result.code==666){
                    layer.alert('添加成功', {icon: 1},function (index) {
                        layer.closeAll();
                        $('#add_user').css("display","none");
                        window.location.reload()

                    });

                }else{
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (XMLHttpRequest.status==555){
                    var layer;
                    layui.use(['layer', 'form'], function(){
                        layer = layui.layer
                        layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                            //do something
                            layer.close(index);
                            top.location.href="/login";
                        });
                    });
                }else{
                    var layer;
                    layui.use(['layer', 'form'], function(){
                        layer = layui.layer
                        layer.alert('页面加载失败',{icon:7}, function(index){
                            layer.close(index);
                        });
                    });
                }
            },
        });
    }

}